package com.uenpay.im.ui.old.activity.movement;

import com.qiyunxin.android.http.utils.StringUtils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.ui.old.activity.dynamic.Reply;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONException;
import org.json.JSONObject;

public class ReplyMovementInvokeItem extends BaseHttpInvokeItem {
	public class ReplyMovementInvokeItemResult extends HttpInvokeResult {
		public Reply reply = new Reply();
	}

	public ReplyMovementInvokeItemResult getOutput() {
		return (ReplyMovementInvokeItemResult) GetResultObject();
	}

	public ReplyMovementInvokeItem(int movementId, String content,String pCustId) {
		String url = APIConfiguration.getBaseUrl()
				+ "Activity/Activity/addReply?activityid=" + movementId + "&content=" +StringUtils.encodeParams(content)
				+ "&pcustid="+pCustId+"&" + APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		ReplyMovementInvokeItemResult result = new ReplyMovementInvokeItemResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		if (result.status != 0) {
			return result;
		}

		JSONObject jo = null;
		try {
			jo = json.getJSONObject("data");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (jo != null) {
			result.reply.replyid = jo.optInt("replyid") + "";
			if (jo.has("pcustid")) {
				result.reply.pcustid = jo.optInt("pcustid");
			}
			if (jo.has("pnickname")) {
				result.reply.pnickname = jo.optString("pnickname");
			}
			result.reply.nickname = IMApplication.config.getUserName();
			result.reply.content = jo.optString("content");
			result.reply.custid = jo.optString("custid");
		}

		return result;
	}
}
